const app = getApp()
import { getStorage,setStorage } from "../../../utils/localStorage"
import { baseUrl } from "../../../api/request"
import { listByDate, previousactivitiesDate } from "../../../api/activity"
import { year, month, day } from "../../../utils/util"
import { activityGive_option, activityOption } from "../../../api/homePage"
import { getMapDistance } from "../../../utils/address"
Component({
    data: {
        navData: {
            title: '乡音活动', //导航栏名称
            level: 3 //导航栏级别  
        },
        imgUrl: baseUrl,
        scFlag: false,
        listQuery: {
            state: true,
            pageSize: 5,
            pageNum: 1,
            userId: null,
            date: "",
        },
        total: 0, //列表总页数
        showPull: true, //上拉加载更多状态
        swiper: {
            autoplay: true,  //自动切换
            dots: true,  //指示点
            dotsColor: "#ffffff", //指示点颜色
            dotsActiveColor: "#4646A0",//选中指示点颜色
            circular: true, //衔接滑动
            duration: 3000, //滑动动画时长
            boxFlag: true,
        },
        list: [
            // {
            //     src: '../../../image/1062.jpg',
            //     title: '果蔬采摘',
            //     time: '10月23日 09:00',
            //     text: '藏在山里的隐世宝藏秘境',
            //     avart: '../../../image/avavt.png',
            //     username: '周末趣徒',
            //     scNum: 1340,
            //     plNum: 203
            // },
        ],
        yearIndex: 0, //right弹窗年份索引
        monthIndex: 0, //right弹窗月份索引
        latlng: {},
        pageTop: app.globalData.navHeight + app.globalData.navTop,
        //获取屏幕下方得底部黑条高度
        bottomLift: app.globalData.bottomLift,
        right: -100,  //弹窗隐藏显示的值
        acTypeList: [], //活动分类列表

    },
    lifetimes: {
        attached() {
            this.setData({
                imgUrl: baseUrl,
                latlng: getStorage('latLng'),
                user: getStorage('user'),
                "listQuery.userId": getStorage('user').id,
            });
            this.getWqDate();
            this.getListByDate();
            this.getDate();
        },
        detached() { }
    },
    pageLifetimes: {
        show() {
            let activityId = app.globalData.activityId,
                activityGive = app.globalData.activityGive,
                activityGiveNum = app.globalData.activityGiveNum,
                activityCtion = app.globalData.activityCtion,
                activityCtionNum = app.globalData.activityCtionNum,
                activityMent = app.globalData.activityMent,
                signUpNum = app.globalData.signUpNum;
            if (activityId) {
                let index = this.data.list.findIndex(item => { return item.id === activityId }),
                    likeValue = "list[" + index + "].likeState",
                    likeNum = "list[" + index + "].likeNumber",
                    ctionValue = "list[" + index + "].collectionState",
                    ctionNum = "list[" + index + "].collectionNumber",
                    commentNum = "list[" + index + "].commentNumber",
                    signNum = "list[" + index + "].peopleNumber";

                this.setData({
                    [likeValue]: activityGive === 1 ? true : false,
                    [likeNum]: activityGiveNum,
                    [ctionValue]: activityCtion === 1 ? true : false,
                    [ctionNum]: activityCtionNum,
                    [commentNum]: activityMent,
                    [signNum]: signUpNum
                });
            };
        },
    },
    methods: {
        //获取活动列表数据
        async getListByDate() {
            let that = this.data;
            listByDate(that.listQuery).then(res => {
              console.log(res,'res');
                if (!res.data.rows || res.data.rows.length === 0) {
                    this.setData({
                        list: [],
                    });
                } else {
                    res.data.rows.forEach(item => {
                        item.imageScale = item.imageScale.split(",");
                        item.distance = getMapDistance(that.latlng.lat, that.latlng.lng, item.latitude, item.longitude);
                    });
                    this.setData({
                        list: [...that.list, ...res.data.rows],
                        total: Math.ceil(res.data.total / that.listQuery.pageSize)
                    });
                };
            })
        },
        //获取往期活动条件筛选日期
        getWqDate() {
            previousactivitiesDate().then(res => {
                this.setData({
                    acTypeList: res.data.data
                });
            });
        },
        //nav导航栏返回箭头按钮点击事件
        navBack() {
            wx.navigateBack();
        },
        //标签切换事件
        navEvent(e) {
            let state = e.currentTarget.dataset.state;
            this.setData({
                list: [],
                yearIndex: 0,
                monthIndex: 0,
                "listQuery.state": state,
                "listQuery.pullFlag": true,
                "listQuery.pageNum": 1,
                "listQuery.date": "",
            });
            this.getListByDate();
        },
        //头部日期点击事件
        riqiClick(e) {
            wx.navigateTo({
                url: '/pagesHuodong/pages/riqi/index',
            })
        },
        //头部活动分类点击事件
        typeClick() {
            this.setData({
                right: 0
            });
        },
        //列表点击事件
        listClick(e) {
            const item = e.currentTarget.dataset.item;
            wx.navigateTo({
                url: '/pagesShouYe/pages/bnnerDetailMore/index?id=' + item.id,
            });
            // if (this.data.listQuery.state) {
            //     setStorage('activityId',item.id);
            //     wx.navigateBack();
            // } else {
            //     wx.navigateTo({
            //         url: '/pagesShouYe/pages/bnnerDetailMore/index?id=' + item.id,
            //     });
            // };

        },
        //活动点赞图标事件
        async collectionClick(e) {
            let that = this.data,
                item = e.currentTarget.dataset.item,
                index = e.currentTarget.dataset.index,
                flag = !item.likeState ? true : false;
            activityGive_option({ userId: that.user.id, activityId: item.id, state: flag }).then(res => {
                let state = "list[" + index + "].likeState",
                    num = "list[" + index + "].likeNumber";
                wx.showToast({
                    icon: 'none',
                    title: res.data.msg,
                })
                this.setData({
                    [state]: flag,
                    [num]: !flag ? item.likeNumber - 1 : item.likeNumber + 1
                });
            });
        },
        //活动收藏图标点击事件
        scTap(e) {
            let that = this.data,
                item = e.currentTarget.dataset.item,
                index = e.currentTarget.dataset.index,
                flag = !item.collectionState ? true : false;
            activityOption({ userId: that.user.id, activityId: item.id, state: flag }).then(res => {
                let state = "list[" + index + "].collectionState",
                    num = "list[" + index + "].collectionNumber";
                if (res.data.code === 200) {
                    wx.showToast({
                        icon: 'none',
                        title: res.data.msg,
                        duration: 1000,
                    })
                    this.setData({
                        [state]: flag,
                        [num]: !flag ? item.collectionNumber - 1 : item.collectionNumber + 1
                    });
                };
            });
        },
        //往期活动条件查询月份列表数据处理
        getDate() {
            let that = this.data;
            let date = new Date(),
                year = date.getFullYear(),
                month = date.getMonth() + 1;
            that.acTypeList.forEach((item, index) => {
                if (item.month === month) {
                    item.state = 1;
                    this.setData({
                        rightIndex: index
                    });
                };
            });
            this.setData({
                acTypeList: that.acTypeList
            });
        },
        //分类弹框点击事件
        boxClick(e) {
            this.setData({
                right: -100
            });
        },
        //往期活动月份列表点击事件
        acListTap(e) {
            let year = e.currentTarget.dataset.year,
                month = e.currentTarget.dataset.month,
                index = e.currentTarget.dataset.index,
                i = e.currentTarget.dataset.i;
            this.setData({
                yearIndex: index,
                monthIndex: i,
                list: [],
                "listQuery.pageNum": 1,
                "listQuery.date": year + "-" + month + "-01",
                right: -100,

            });
            this.getListByDate();
        },
        //地址点击事件
        navigationTap(e) {
            let item = e.currentTarget.dataset.item;
            wx.openLocation({
                latitude: Number(item.latitude),
                longitude: Number(item.longitude),
            });
        },
        //往期活动日期筛选刷新按钮操作
        refresh() {
            this.setData({
                list: [],
                yearIndex: 0,
                monthIndex: 0,
                "listQuery.pageNum": 1,
                "listQuery.date": "",
                right: -100,
            });
            this.getListByDate();
        },
        /* 上拉加载更多 */
        pullUp(e) {
            let that = this.data;
            if ((that.listQuery.pageNum + 1) <= that.total) {
                this.setData({
                    showPull: false,
                    "listQuery.pageNum": that.listQuery.pageNum += 1
                });
                this.getListByDate();
            } else {
                this.setData({
                    showPull: true
                });
            };
        },
    }
})